package com.rz.paas.baselib.netRequest.request;


import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

public class UrlCreator {

    public static String createUrlFromParams(String url, Map<String, Object> params) {


        StringBuilder builder = new StringBuilder();

        builder.append(url);
        if (url.indexOf("?") > 0 || url.indexOf("&") > 0) {
            builder.append("&");
        } else {
            builder.append("?");
        }

        for (Map.Entry<String, Object> entry : params.entrySet()) {

            try {
                String value = URLEncoder.encode(entry.getValue().toString(), "UTF-8");
                builder.append(entry.getKey()).append("=").append(value).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        builder.deleteCharAt(builder.length() - 1);
        Log.d("UrlCreator","UrlCreator::createUrlFromParams: url:" + builder.toString());
        return builder.toString();
    }
}
